package com.java.design.spring.beans;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MuTablePropertyValues {

    private final List<PropertyValue> propertyValueList;


    public MuTablePropertyValues(List<PropertyValue> propertyValueList) {
        if (propertyValueList == null) {
            this.propertyValueList = new ArrayList<>();
        } else {
            this.propertyValueList = propertyValueList;
        }
    }

    public MuTablePropertyValues() {
        this.propertyValueList = new ArrayList<>();
    }

    public PropertyValue[] getPropertyValues() {
        //将集合转化返回
        return this.propertyValueList.toArray(new PropertyValue[0]);
    }

    //根据name名称获取 propertyValue对象
    public PropertyValue getPropertyValue(String name) {
        for (PropertyValue propertyValue : this.propertyValueList) {
            if (propertyValue.getName().equals(name)) return propertyValue;
        }
        return null;
    }

    //判断集合是否为空
    public boolean isEmpty() {
        return this.propertyValueList.isEmpty();
    }

    //添加集合数据
    public MuTablePropertyValues add(PropertyValue value) {
        for (int i = 0; i < propertyValueList.size(); i++) {
            PropertyValue currPv = propertyValueList.get(i);
            if (currPv.getName().equals(value.getName())) {
                propertyValueList.set(i, value);
                return this;
            }
        }
        propertyValueList.add(value);
        return this;
    }

    //判断是否有指定属性值
    public boolean contains(String propertyName) {
        return getPropertyValue(propertyName) != null;
    }

    public Iterator<PropertyValue> iterable() {
        Iterator<PropertyValue> iterator = propertyValueList.iterator();
        return this.propertyValueList.iterator();
    }
}
